package com.cskaoyan.controller.wx;/**
 * @author mj
 * @date 2023-01-02 00:25
 * @describe
 */

import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.marketBean.MarketAddress;
import com.cskaoyan.bean.marketBean.MarketUser;
import com.cskaoyan.service.wx.WXAddressService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author MJ
 * @date 2023-01-02 00:25
 * @describe
 */
@RestController
@RequestMapping("wx")
public class MJAderssController {
    @Autowired
    WXAddressService wxAddressService;

    @RequestMapping("address/list")
    public BaseRespVo list(){
        CommonData<MarketAddress> marketAddressCommonData = new CommonData<>();
        try {
            marketAddressCommonData =wxAddressService.list(marketAddressCommonData);
        }catch (Exception e){
            return BaseRespVo.exception("数据库异常");
        }
        return BaseRespVo.okyjc(marketAddressCommonData);
    }
    @RequestMapping("address/save")
    public BaseRespVo userAddressSave(@RequestBody MarketAddress param) {
      if (param.getId() == 0) {
            try {
                Subject subject = SecurityUtils.getSubject();
                MarketUser principal = (MarketUser) subject.getPrincipal();
                Integer userId = principal.getId();
                param.setUserId(userId);
                wxAddressService.userAddressCreate(param);
            } catch (Exception e) {
                return BaseRespVo.exception("添加地址异常");
            }
            return BaseRespVo.okyjc(null);
        }
        MarketAddress result;
      try {
           result =wxAddressService.userAddressUpdate(param);
      }catch (Exception e){
          return BaseRespVo.exception("修改地址异常");
      }
      return BaseRespVo.okyjc(result);
    }

    @RequestMapping("address/detail")
    public BaseRespVo userAddressUpdate(String id){
        MarketAddress result;
        try {
           result = wxAddressService.selectAddress(id);
        }catch (Exception e) {
            return BaseRespVo.exception("查询地址异常");
        }
        return BaseRespVo.okyjc(result);
    }

    @RequestMapping("address/delete")
    public BaseRespVo userAddressDelete(@RequestBody MarketAddress param){
        try {
            wxAddressService.deleteAddress(param.getId());
        }catch (Exception e){
            return BaseRespVo.exception("删除异常");
        }
        return BaseRespVo.okyjc(null);
    }
}
